var vue = new Vue({
    el: "#main",
    data() {
        return {
            tabIndex: 0, //0:消防安全检查，1:隐患问题待整改
            total: 0, //总数
            monitorCommunityList: [], //列表
            id: "", //展开数据的id
            searchVal: '',
            year: '',
            yearArr: [],
            month: 0, //月份
            days: 0, //距离当月结束还剩的天数
            rate: '', //顶部季度走访率
            rateIndex: 0, //顶部季度走访率索引
            rateArr: 0, //走访路数组
            searchLoading: false,
            announcementList: [], //通知公告
            isEmpty: false,
            loading: false, //非管理员
            finished: false, //非管理员
            isGrid: false,
            page: 1,
            grid_id: '',
            is_grid: false,
            list: [],
            rectificationList: [],
        }
    },
    created() {
        this.getTime()
    },
    mounted() {
        this.is_grid = getRequest().is_grid
        if (this.is_grid == 0) {
            this.getCommunityList()
        } else if (this.is_grid == 1) {
            this.grid_id = getRequest().grid_id
            this.isGrid = true
            this.getList()
        }

        window.addEventListener("visibilitychange", () => {
            if (document.hidden) {

            } else {
                this.getTime()
                this.is_grid = getRequest().is_grid
                if (this.is_grid == 0) {
                    this.getCommunityList()
                } else if (this.is_grid == 1) {
                    this.grid_id = getRequest().grid_id
                    this.isGrid = true
                    this.getList()
                }
            }
        })
    },
    methods: {
        // 获取时间
        getTime() {
            let time = new Date()
            this.year = time.getFullYear()
            let currentMonth = time.getMonth() + 1;
            this.month = time.getMonth() + 1;

            var nextMonthFirstDay = new Date(this.year, currentMonth, 1);
            var currentMonthLastDay = new Date(nextMonthFirstDay.getTime() - 1);
            this.days = currentMonthLastDay.getDate() - time.getDate();
            if (currentMonth >= 1 && currentMonth <= 3) {
                this.rateIndex = 0;
            } else if (currentMonth >= 4 && currentMonth <= 6) {
                this.rateIndex = 1;
            } else if (currentMonth >= 7 && currentMonth <= 9) {
                this.rateIndex = 2;
            } else {
                this.rateIndex = 3;
            }
            let _year = this.year - 2022
            for (let i = 0; i <= _year; i++) {
                this.yearArr.push(this.year - i)
            }
        },
        // 获取管理员列表
        getCommunityList() {
            let time = new Date()
            let year = time.getFullYear()
            if (this.tabIndex == 0) {
                year = this.year
            }

            ajax({
                url: formal + '/place-check/get-grids?keywords=' + this.searchVal + '&year=' + year + '&is_standard=' + this.tabIndex + '&season=',
                type: 'GET',
                success: (res) => {
                    this.searchLoading = false
                    this.total = res.data.place_total
                    let seasonArr = res.data.seasonArr
                    for (let i = 0; i < seasonArr.length; i++) {
                        seasonArr[i] = i + 1 + '季度走访率：' + seasonArr[i] + '%'
                    }
                    this.rateArr = seasonArr
                    this.rate = this.rateArr[this.rateIndex]
                    res.data.list.map(item => {
                        let arr = item.seasonArr
                        for (let i = 0; i < arr.length; i++) {
                            arr[i] = i + 1 + '季度走访率：' + arr[i] + '%'
                        }
                        item.rateArr = arr
                        item.rateIndex = this.rateIndex
                        item._care_rate = item.rateArr[item.rateIndex]

                        item.child.map(e => {
                            let arr1 = e.seasonArr
                            for (let i = 0; i < arr1.length; i++) {
                                arr1[i] = i + 1 + '季度走访率：' + arr1[i] + '%'
                            }
                            e.rateArr = arr1
                            e.rateIndex = this.rateIndex
                            e._care_rate = e.rateArr[e.rateIndex]
                        })
                    })
                    this.monitorCommunityList = res.data.list
                    if (this.announcementList.length == 0 && (this.month == 3 || this.month == 6 || this.month == 9 || this.month == 12)) {

                        if (res.data.need_check.length > 30) {
                            this.announcementList = res.data.need_check.slice(0, 30)
                        } else {
                            this.announcementList = res.data.need_check
                        }
                        if (this.announcementList.length >= 1) {
                            setTimeout(() => {
                                new Swiper(".mySwiper", {
                                    direction: "vertical",
                                    loop: true,
                                    autoplay: {
                                        delay: 1500, //自动切换的时间间隔，单位ms
                                        disableOnInteraction: false //用户操作swiper之后，是否禁止autoplay
                                    },
                                });
                            }, 500)
                        }
                    }
                }
            })
        },
        // 获取管理员已整改数据
        getRectification() {
            ajax({
                url: formal + '/place-check/get-rectification',
                type: 'GET',
                success: (res) => {
                    res.forEach(element => {
                        element.flage = false
                    });
                    this.rectificationList = res
                }
            })
        },
        // 获取非管理员的场所
        getList() {
            let time = new Date()
            let year = time.getFullYear()
            if (this.tabIndex == 0) {
                year = this.year
            }
            ajax({
                url: formal + '/place-check/get-grids-place?grids_id=' + this.grid_id + '&page=' + this.page + '&keywords=' + this.searchVal + '&is_standard=' + this.tabIndex + '&year=' + year,
                type: 'GET',
                success: (res) => {
                    this.total = res.data.total
                    let seasonArr = res.data.seasonArr
                    for (let i = 0; i < seasonArr.length; i++) {
                        seasonArr[i] = i + 1 + '季度走访率：' + seasonArr[i] + '%'
                    }
                    this.rateArr = seasonArr
                    this.rate = this.rateArr[this.rateIndex]
                    this.list = this.list.concat(res.data.list)
                    // this.list = res.data.list

                    if (this.list.length == 0) {
                        this.isEmpty = true
                    } else {
                        this.isEmpty = false
                    }
                    // 数据全部加载完成
                    if (res.data.list.length == 0) {
                        this.finished = true;
                    }
                    this.page++;
                    this.loading = false;

                    if (this.announcementList.length == 0) {
                        if (res.data.need_check.length > 30) {
                            this.announcementList = res.data.need_check.slice(0, 30)
                        } else {
                            this.announcementList = res.data.need_check
                        }
                        if (this.announcementList.length >= 1) {
                            setTimeout(() => {
                                new Swiper(".mySwiper", {
                                    direction: "vertical",
                                    loop: true,
                                    autoplay: {
                                        delay: 1500, //自动切换的时间间隔，单位ms
                                        disableOnInteraction: false //用户操作swiper之后，是否禁止autoplay
                                    },
                                });
                            }, 500)
                        }
                    }
                }
            })
        },
        // 触底加载
        onLoad() {
            this.getList()
        },
        // 改变tab
        changeTab(val) {
            if (this.tabIndex == val) {
                return
            }
            this.tabIndex = val
            this.id = ''

            if (this.is_grid == 1) {
                this.page = 1
                this.list = []
                this.getList()
            } else if (this.is_grid == 0) {
                if (this.tabIndex == 2) {
                    this.getRectification()
                } else {
                    this.getCommunityList()
                }

            }


        },
        // 搜索
        searchChange() {
            if (this.searchVal == '') {
                if (this.is_grid == 1) {
                    this.page = 1
                    this.list = []
                    this.getList()
                } else if (this.is_grid == 0) {
                    if (this.tabIndex == 2) {
                        this.getRectification()
                    } else {
                        this.getCommunityList()
                    }
                }

            }
        },
        search() {
            if (this.searchVal == '') return;
            this.searchLoading = true
            if (this.is_grid == 1) {
                this.page = 1
                this.list = []
                this.getList()
            } else if (this.is_grid == 0) {
                if (this.tabIndex == 2) {
                    this.getRectification()
                } else {
                    this.getCommunityList()
                }
            }
        },
        // 改变年份
        changeYear() {
            if (this.is_grid == 1) {
                this.page = 1
                this.list = []
                this.getList()
            } else if (this.is_grid == 0) {
                if (this.tabIndex == 2) {
                    this.getRectification()
                } else {
                    this.getCommunityList()
                }
            }
        },
        // 改变社区季度
        changeCountyRate(e, val) {
            if (val == -1) {
                if (e.rateIndex == 0) {
                    e.rateIndex = 3
                } else {
                    e.rateIndex -= 1
                }
                e._care_rate = e.rateArr[e.rateIndex]

            } else {
                if (e.rateIndex == 3) {
                    e.rateIndex = 0
                } else {
                    e.rateIndex += 1
                }
                e._care_rate = e.rateArr[e.rateIndex]
            }
        },
        // 改变网格季度
        changeGridRate(e, val) {
            if (val == -1) {
                if (e.rateIndex == 0) {
                    e.rateIndex = 3
                } else {
                    e.rateIndex -= 1
                }
                e._care_rate = e.rateArr[e.rateIndex]

            } else {
                if (e.rateIndex == 3) {
                    e.rateIndex = 0
                } else {
                    e.rateIndex += 1
                }
                e._care_rate = e.rateArr[e.rateIndex]
            }
        },
        // 点击警告去检查
        goPlaceDetail(e) {
            let id = e.target.dataset.id
            if (id) {
                location.href = '/placeDetail.html?id=' + id
            }
        },
        // 前往非管理员的场所详情
        gorRecordDetails(uuid, id) {
            if (this.tabIndex == 0) {
                location.href = "../placeDetail.html?id=" + uuid;
            } else if (this.tabIndex == 1 || this.tabIndex == 2) {
                location.href = "../recordDetails.html?id=" + id + "&status=" + 1 + "&is_standard=" + this.tabIndex + "&type=2";
            }

        },
        // 前往管理员看网格员的场所列表
        handleMonitorMarker(id) {
            if (this.tabIndex == 0) {
                location.href = "../placeList.html?id=" + id + "&type=" + 2 + "&keywords=" + this.
                searchVal;
            } else if (this.tabIndex == 1) {
                location.href = "../hazardReminder.html?id=" + id + "&status=" + 1 + "&keywords=" + this.
                searchVal + "&tabIndex=" + this.
                tabIndex;
            } else if (this.tabIndex == 2) {
                location.href = "../hazardReminder.html?id=" + id + "&status=" + 1 + "&keywords=" + this.
                searchVal + "&tabIndex=" + this.
                tabIndex;
            }
        },

        handleRectification(year, season) {

            location.href = "../hazardReminder.html?status=" + 1 + "&keywords=" + this.
            searchVal + "&tabIndex=" + this.
            tabIndex + "&yearS=" + year + "&season=" + season;
        },
        changeRate(val) {

            if (val == -1) {
                if (this.rateIndex == 0) {
                    this.rateIndex = 3
                } else {
                    this.rateIndex -= 1
                }
                this.rate = this.rateArr[this.rateIndex]

            } else {
                if (this.rateIndex == 3) {
                    this.rateIndex = 0
                } else {
                    this.rateIndex += 1
                }
                this.rate = this.rateArr[this.rateIndex]
            }
        }

    }
})


questions = [{
    oneTitle: '基础信息',
    questions: [{
        isSystem: false, //是否是系统题
        content: '你的性别是?', //题干
        contentTip: '', //提示信息
        type: '', //题目类型：0:单选,1:多选，2:填空
        layoutType: 'layout_one', //layout_one:布局一，layout_two：布局二
        chooseItem: [{
            content: '选项1', //选项内容
            picture: '图片地址', //图片地址
            contentTips: '', //提示信息
            unit: '', //单位
        }],
        aboutQuestions: [{
            content: '', //关联题目的题干
            chooseItem: [], //关联题目的选项
            choose: [], //关联题目的选中选项
        }]
    }]
}]